package at.tuwien.seminar.scrum1.view.customer.create;

import java.util.Map;

import at.tuwien.seminar.scrum1.model.Bill;
import at.tuwien.seminar.scrum1.model.Room;
import at.tuwien.seminar.scrum1.proxies.CustomerProxy;
import at.tuwien.seminar.scrum1.proxies.ReservationProxy;
import at.tuwien.seminar.scrum1.view.bill.list.BillList;
import at.tuwien.seminar.scrum1.view.reservation.list.ReservationList;
import at.tuwien.seminar.scrum1.view.room.list.RoomList;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextArea;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.validation.validator.EmailAddressValidator;
import org.apache.wicket.validation.validator.NumberValidator;
import org.apache.wicket.validation.validator.StringValidator;
import org.apache.wicket.validation.validator.UrlValidator;

import at.tuwien.seminar.scrum1.model.Customer;
import at.tuwien.seminar.scrum1.view.customer.list.CustomerList;

public class CreateCustomer extends WebPage {
	
	public CreateCustomer()
	{
        add(new Link<Room>("roomLink") {

			@Override
			public void onClick() {
				setResponsePage(new RoomList());
			}

		});
		add(new Link<Customer>("customerLink") {

			@Override
			public void onClick() {
				setResponsePage(new CustomerList());
			}

		});

        add(new Link<Customer>("reservationLink") {

			@Override
			public void onClick() {
				setResponsePage(new ReservationList(ReservationProxy.getInstance().getAll()));
			}

		});

         add(new Link<Bill>("billLink") {

			@Override
			public void onClick() {
				setResponsePage(new BillList());
			}

		});

		Customer newCustomer = new Customer();
		Form<Customer> form = new Form<Customer>("customerForm", new CompoundPropertyModel<Customer>(newCustomer))
		{
			@Override
			protected void onSubmit()
			{
                CustomerProxy.getInstance().insertCustomer(getModelObject());
				setResponsePage(new CustomerList());
			}
		};
		
		form.add(new TextField<String>("firstName").
				setRequired(true).
				add(StringValidator.maximumLength(40)));
		form.add(new TextField<String>("lastName").
				setRequired(true).
				add(StringValidator.maximumLength(40)));
		form.add(new TextField<String>("companyName").
				setRequired(false).
				add(StringValidator.maximumLength(40)));
		form.add(new TextArea<String>("notes").
				setRequired(true).
				add(StringValidator.maximumLength(100)));
		form.add(new TextField<Double>("discount").
				setRequired(true).
				// replace!
				add(NumberValidator.range(0,100)));
		form.add(new TextField<String>("phone").
				setRequired(true).
				add(StringValidator.maximumLength(30)));
		form.add(new TextField<String>("email").
				setRequired(true).
				add(EmailAddressValidator.getInstance()));
		form.add(new TextField<String>("weburl").
				setRequired(true).
				add(new UrlValidator()));
		form.add(new TextField<String>("fax").
				setRequired(true).
				add(StringValidator.maximumLength(40)));
		form.add(new Button("submit"));
		form.add(new FeedbackPanel("feedback"));
		
		add(form);
		
		add(new Link<Customer>("back") {
			@Override
			public void onClick() {
				setResponsePage(new CustomerList());

			}
		});
	}
}
